package applet;
import java.util.ArrayList;

import musicbrainz.*;

import processing.core.PApplet;
import processing.core.PImage;

class ReleaseMenu extends GuiElement
{
	public PImage icon1,icon2;
	public  String label;
	public  int fontSize;
	public int dragDownSize = 10;
	public boolean color = false;
	public boolean fixedSize = true;
	public VerticalScrollBar selectBar;
	public Button upButton, downButton;
	public boolean onUpButton, onDownButton;
	
	public float alphaVal=255;
	public ArrayList<ReleaseButton> items;
	
	public static boolean artistStatusChanged;
	
	private boolean justPressed = false;
	public boolean onItem = false;
	public int onItemIndex = 0;
	public int prevItemIndex=0;
	
	public ReleaseMenu(ArrayList<ReleaseButton> subMenu)
	{
		fixedSize = true;
		height = 20;
		fontSize = 16;
		items = subMenu;
		
		upButton = new Button();
		downButton = new Button();
		upButton.fixedSize = false;
		downButton.fixedSize = false;
		//upButton.width = 20;
		//downButton.width = 20;
		//upButton.setLabel("");
		//downButton.setLabel("");
		upButton.setIcon(GLOBAL.baseDir + "uparrow.png");
		downButton.setIcon(GLOBAL.baseDir + "downarrow.png");
		upButton.height = 18;
		downButton.height = 18;
	}
	
	public ReleaseMenu(int cx, int cy, ArrayList<ReleaseButton> items2)
	{
		fixedSize = true;
		height = 20;
		items = items2;
		fontSize = 16;
		x = cx;
		y = cy;
		
		upButton = new Button();
		downButton = new Button();
		upButton.fixedSize = false;
		downButton.fixedSize = false;
		//upButton.width = 20;
		//downButton.width = 20;
		//upButton.setLabel("");
		//downButton.setLabel("");
		upButton.setIcon(GLOBAL.baseDir + "uparrow.png");
		downButton.setIcon(GLOBAL.baseDir + "downarrow.png");
		upButton.height = 18;
		downButton.height = 18;
	}

	public void setIcon(String name1,String name2)
	{
		if(name1 != null && name2!=null)
		{
			icon1 = GLOBAL.processing.loadImage(name1); //down
			icon2 = GLOBAL.processing.loadImage(name2); //up
			
		}
	}

	public void setLabel(String name)
	{
		label = name;
		GLOBAL.processing.textFont(GLOBAL.tFont, fontSize);
		
		if(fixedSize)
		{
			width = GLOBAL.defaultMenuWidth - 18;
			if(GLOBAL.processing.textWidth(name)>width)
				fontSize = (int) (2 * width / name.length()) -8;
			if(fontSize==0)
				fontSize = 1;
		}
		else
			width = (int)(GLOBAL.processing.textWidth(name) + 10);
		
		if(icon1 != null || icon2!=null){width += 18;}
		
		if(/*GLOBAL.getItemsSize()*/items.size()> dragDownSize)
		{
			selectBar = new VerticalScrollBar();
			selectBar.x = x + width + 7;
			selectBar.y = y + height + 20;	
			selectBar.width = 14;
			selectBar.height = items.get(0).height * dragDownSize - 40;	
			selectBar.size = (float)1 / 10;
			
			upButton.x = x + width +5;
			upButton.y = y + height;
			downButton.x = x + width + 5;
			downButton.y = selectBar.y + selectBar.height;
		}
	}

	public void draw()
	{
		if(onUpButton && upButton.mouseOver())
		{
			selectBar.value -= 0.5/(float)items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/;
			if(selectBar.value<0)
				selectBar.value = 0;
		}
		else if(onDownButton && downButton.mouseOver())
		{
			selectBar.value += 0.5/(float)items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/;
			if(selectBar.value>0.9)
				selectBar.value = (float)0.9;
		}
		
		if(artistStatusChanged)
		{
			items.get(GLOBAL.curReleaseId).active = false;
			GLOBAL.curReleaseId = 0;
			//items.get(GLOBAL.curTrackId).active=true;
			
			
			//load all the release data into items
			ArrayList<ReleaseButton> releaseButtons1 = new ArrayList<ReleaseButton>();
			ReleaseArrayMB releaseArray1= GLOBAL.http.getMBReleaseInfoFromArtist(GLOBAL.testArtist.mbid, 200, 0);
			for(int i=0; i<releaseArray1.arr.size(); i++)
			{
				ReleaseMB tmp = releaseArray1.arr.get(i);
				Release b = new Release(tmp.mbid,tmp.status, tmp.title, tmp.quality, tmp.language,tmp.script,tmp.date,tmp.country);
				ReleaseButton a = new ReleaseButton();
				a.release = b;
				a.setLabel(b.title);
				releaseButtons1.add(a);
			}
			items = releaseButtons1;
			
			artistStatusChanged = false;
		}	
		GLOBAL.curRelease = items.get(GLOBAL.curReleaseId).release;
		items.get(GLOBAL.curReleaseId).active = true;
		showSubs();
		
		//PApplet.println("release "+onItem + " "+prevItemIndex+" "+GLOBAL.curReleaseId);
		
		//TODO: draw release info , and all the track info with the release 
		//if(onItem)
		//	items.get(onItemIndex).drawTag();
		//else if(prevItemIndex>=0)
		//	items.get(prevItemIndex).drawTag();
		items.get(GLOBAL.curReleaseId).drawTag();
	}

	public void setFontSize(int size) {
		fontSize = size;
	}

	
	public void showSubs()
	{
		float val = 0;
		if(items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/>dragDownSize)
		{
			selectBar.alphaVal = alphaVal;
			selectBar.draw();
			upButton.draw();
			downButton.draw();
			// Find from what ArtistButton we have to print, based on the value of the scrollbar in this moment
			val = PApplet.map(selectBar.value,0, 1- selectBar.size,0,items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/ - dragDownSize +1);
		}
		else
			val = 0;
		// Draw all the visible series ArtistButtons

		for(int j = 0; j<items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/; j++) {
			if( j >= (int)val && j < (int)val + dragDownSize && j < items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/) 
			{
				items.get(j).x = x;
				items.get(j).y = y +(j-(int)val+1)*height;
				//items.get(j).active = true;
				items.get(j).width = width;
				items.get(j).height = height - 2;
				//items.get(j).setLabel(GLOBAL.categoryNames[j]);
				//items.get(j).setIcon(GLOBAL.baseDir + ""+GLOBAL.categoryNames[j].toLowerCase() +".png");
				items.get(j).alphaVal = alphaVal;
				items.get(j).draw();
			}
			else {
				items.get(j).x = 2000;
				items.get(j).y = 2000;
			}
			
		}
	}
	
	public void mouseReleased()
	{
		//if (mouseOver() && justPressed)
		//{
		//	active = !active;
		//}
		if(items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/>dragDownSize)
		selectBar.mouseReleased();

		if(onItem && onItemIndex>=0)
		{
			if(items.get(onItemIndex).mouseOver())
			{
				//GLOBAL.curTrackId = 0; //yes it's track?????????????????/
				TrackMenu.releaseStatusChanged = true;
				items.get(onItemIndex).active = true;
				if(GLOBAL.curReleaseId != onItemIndex)
					items.get(GLOBAL.curReleaseId).active = false;
				GLOBAL.curReleaseId = onItemIndex;
				GLOBAL.curRelease = items.get(GLOBAL.curReleaseId).release;
			}
		}

		justPressed = false;
		onUpButton = onDownButton = false;
	}
	public void mousePressed()
	{
		justPressed = true;
		
		if(upButton.mouseOver())
			onUpButton = true;
		else if(downButton.mouseOver())
			onDownButton = true;
	}
	
	
	@Override
	public boolean mouseOver()
	{
		float val = 0;
		// Find from what ArtistButton we have to print, based on the value of the scrollbar in this moment
		if(items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/>dragDownSize)
			val = PApplet.map(selectBar.value,0, 1-selectBar.size ,0,items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/ - dragDownSize +1);
		else 
			val = 0;
				
		//if(GLOBAL.processing.mouseX >= x && GLOBAL.processing.mouseX <= x + width && 
    	//		GLOBAL.processing.mouseY >= y && GLOBAL.processing.mouseY <= y + height) return true;
		//else
		//{
		for(int j= (int)val; j < (int)val + dragDownSize && j < items.size()/*GLOBAL.curReleaseButtonsPerArtist.size()*/; j++) 
		{
			if(items.get(j).mouseOver())
			{
				onItem = true;
				if(onItemIndex>=0)
					prevItemIndex = onItemIndex;
				onItemIndex = j;
				
				return true;
			}
		}
		//}
		onItem = false;
		//if(onItemIndex>=0)
		//	prevItemIndex = onItemIndex;
		onItemIndex = -1;
    	return false;
		
	}

}